/*
 * @Author: GuitarXiaohe && 2205227953@qq.com
 * @Date: 2024-01-13 19:59:36
 * @LastEditors: 18163104359 2205227953@qq.com
 * @LastEditTime: 2024-01-25 15:10:31
 * @FilePath: /umi/src/components/xh-components/xh-button/index.jsx
 * @Description:
 * QQ:2205227953,邮箱:2205227953@qq.com
 * Copyright (c) 2024 by GuitarXiaohe, All Rights Reserved.
 */
import { Button, Modal } from "antd";
import "./index.less";
import { connect } from "umi";
import { useEffect } from "react";
import { PlusOutlined, FormOutlined, DeleteOutlined, SearchOutlined } from "@ant-design/icons";
const { confirm } = Modal;
const XHbutton = ({ list, size = 20, user }) => {
  const init = () => {
    // 校验数据配置
    if (!Array.isArray(list)) {
      console.error(
        new Error(`xh-button组件错误:list属性类型,错误应该为Array而不是${typeof list}`)
      );
      return [];
    }

    if (list.length <= 0) {
      console.warn(`xh-button组件警告:list属性类型数据为空!`);
      return [];
    }
    // 最高权限
    // user.permissions == "*:*:*"; user.permissions == "*:*:*" ||
    return list.filter(
      (t) => user.permissions == "*:*:*" || user.permissions.includes(t.permissions)
    );
  };

  // 匹配数值
  const p = [
    {
      text: "新增",
      icon: <PlusOutlined />,
    },
    {
      text: "修改",
      icon: <FormOutlined />,
    },
    {
      text: "删除",
      icon: <DeleteOutlined />,
    },
    {
      text: "查看",
      icon: <SearchOutlined />,
    },
  ];

  // 匹配icon
  const icon = (val) => {
    const r = p.find((t) => t.text == (val.label || val.text));
    if (r) return r.icon;
  };

  return (
    <div className="btnClass">
      {init().map((t, ind) => (
        <Button
          className="btn"
          key={"XHbutton" + ind}
          danger={t.danger}
          type={t.btnType || "primary"}
          icon={t.icon || icon(t)}
          disabled={t.disabled}
          onClick={() => {
            if (t.showMsg) {
              confirm({
                okText: t.okText || "确认",
                cancelText: t.cancelText || "取消",
                title: t.showMsgTitle || "系统提示",
                icon: t.showMsgIcon || false,
                content: t.content || "是否删除该数据?",
                onOk() {
                  t.emit();
                },
              });
            } else {
              t.emit();
            }
          }}
        >
          {t.text || t.label}
        </Button>
      ))}
    </div>
  );
};

export default connect((state) => ({ user: state.user }))(XHbutton);
